//
//  WelComeViewController.m
//  weibo-OC
//
//  Created by diyuyma on 16/3/17.
//  Copyright © 2016年 zhangkaiyun. All rights reserved.
//

#import "WelComeViewController.h"
#import "UserAccountViewModel.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"

@interface WelComeViewController()
@property (nonatomic, strong)UIImageView *iconImage;
@property (nonatomic, strong)UIImageView *backImage;
@property (nonatomic, strong)UILabel *welcomeLabel;
@end

@implementation WelComeViewController

- (void)loadView {
    [super loadView];
    self.view = self.backImage;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpUI];
}

//不要再viewDidLoad方法中去做动画
- (void)viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    [self startAnimation];
}

- (void)startAnimation {
    
    //usingSpringWithDamping  弹簧系数  0~1之间  越小越弹
    //initialSpringVelocity 加速度
    //弹簧效果 和加速度之间的关系: 弹簧效果 * 10 = 加速度
    
    //外部改变约束
    [self.iconImage mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).with.offset(-kScreenHeight+180);
    }];
    
    /*
     一般在自动布局中使用动画效果  不建议直接修改非让阿门
     
     自动布局系统 会根据设置的约束 在layoutSubViews方法中自动修改frame
     frame 是有自动布局系统来控制的
     如果希望能够提前刷新修改的约束效果  需要调用 layoutIfNeeded() 让约束提前刷新 ,更新frame
     */
    
    //动画效果 由近及远 涉及到加速度
    [UIView animateWithDuration:1.2 delay:0 usingSpringWithDamping:0.9 initialSpringVelocity:9.8 options:0 animations:^{
        //闭包内部 强制刷新页面
        //将提前收集到的约束的变化 进行更新
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.6 animations:^{
            self.welcomeLabel.alpha = 1;
        } completion:^(BOOL finished) {
            //发出通知
            [[NSNotificationCenter defaultCenter]postNotificationName:AppSwitchRootViewControler object:nil];
        }];
    }];
}

//MARK: 添加子视图
- (void)setUpUI {
    // 1.添加子视图
    [self.view addSubview:self.iconImage];
    [self.view addSubview:self.welcomeLabel];
    
    // 2.设置布局
    [self.iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.bottom.equalTo(self.view.mas_bottom).with.offset(-180);
        make.width.mas_equalTo(90);
        make.height.mas_equalTo(90);
    }];

    [self.welcomeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconImage.mas_bottom).with.offset(20);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    
    // 加载图片
    [self.iconImage sd_setImageWithURL:[[UserAccountViewModel alloc] init].headURL placeholderImage:[UIImage imageNamed:@"avatar_default_big"]];

    // 给图片处理成圆角
    self.iconImage.layer.cornerRadius = 45;
    self.iconImage.layer.masksToBounds = true;
}

//懒加载所有的视图控件
//头像
- (UIImageView *)iconImage {
    
    if (_iconImage == nil) {
        _iconImage = [[UIImageView alloc] init];
    }
    return _iconImage;
}

//背景视图
- (UIImageView *)backImage {
    
    if (_backImage == nil) {
        _backImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ad_background"]];
    }
    return _backImage;
}

- (UILabel *)welcomeLabel {
    if (_welcomeLabel == nil) {
        _welcomeLabel = [[UILabel alloc] init];
        _welcomeLabel.alpha = 0;
        [_welcomeLabel initWithLabel:[NSString stringWithFormat: @"%@欢迎回来 ", [[UserAccountViewModel alloc] init].userName] WithColor:[UIColor lightGrayColor] WithSize:19 WithMargin:0];
    }
    return _welcomeLabel;
}


@end
